const express = require("express")
const router = express.Router()
const time = require("../../utils/time")
const db = require("../../sqlServer/dbConnect")

// 食品原料-- 查询
router.post("/source/info", (req, res) => {
    const { values,pageConfig,doName } = req.body;
    const {page,pageSize} = pageConfig
    const s = values === null ? [] :  [values.fname,values.fprovider,values.verify]
    let sql = values === null ? "select * from source limit ?,?; select * from source" : 
    `select * from source where name=? or provider=? or verify=? limit ?,?;
    select * from source where name=? or provider=? or verify=?`
    const data = values === null ? [(Number(page)-1)*Number(pageSize),Number(pageSize)] : 
    [...s,(Number(page)-1)*Number(pageSize),Number(pageSize),...s]
    db.sqlQuery(sql,data,(err, result) => {
        if (err) {
            console.log(err)
            res.send({
                // data: result,
                msg: "获取原料信息失败",
                status: 203
            })
        }else {
            res.send({
                data: result[0],
                total:result[1].length,
                msg: "获取原料信息成功",
                status: 200
            })
        }
    })
    db.sqlQuery("insert into log(doName,doInfo,doTime) values(?,?,?)",[doName,"食品原料查询",time.getData()])
})

// 食品原料-- 添加
router.post("/source/add", (req, res) => {
    const { name, purchase, verify, source, storage, provider,doName } = req.body
    const screatetime = time.getDay()
    let sql = "insert into source(name, purchase, verify, source, storage, provider,screatetime) values(?,?,?,?,?,?,?)"
    const data = [name, purchase, verify, source, storage, provider,screatetime]
    db.sqlQuery(sql, data, (err) => {
        if (err) {
            console.log(err)
            res.send({
                msg: "没有该食品的信息，请添加基本信息！",
                status: 203
            })
        } else {
            res.send({
                msg: "添加成功",
                status: 200
            })
        }
    })
    db.sqlQuery("insert into log(doName,doInfo,doTime) values(?,?,?)",[doName,"食品原料添加",time.getData()])
})

// 食品原料-- 编辑
router.post("/source/edit", (req, res) => {
    const { name, purchase, verify, source, storage, provider, Id,doName} = req.body
    let sql = "update source set name=?,purchase=?,verify=?,source=?,storage=?,provider=? where Id=?"
    const data = [name, purchase, verify, source, storage, provider, Id]
    db.sqlQuery(sql, data, (err) => {
        if (err) {
            console.log(err)
            res.send({
                msg: "编辑失败",
                status: 203
            })
        } else {
            res.send({
                msg: "编辑成功",
                status: 200
            })
        }
    })
    db.sqlQuery("insert into log(doName,doInfo,doTime) values(?,?,?)",[doName,"食品原料编辑",time.getData()])
})

// 食品原料-- 删除
router.post("/source/delete", (req, res) => {
    const { Id,doName } = req.body
    let sql = "delete from source where Id=?"
    const data = [Id]
    db.sqlQuery(sql, data, (err) => {
        if (err) {
            console.log(err)
            res.send({
                msg: "删除失败",
                status: 203
            })
        } else {
            res.send({
                msg: "删除成功",
                status: 200
            })
        }
    })
    db.sqlQuery("insert into log(doName,doInfo,doTime) values(?,?,?)",[doName,"食品原料删除",time.getData()])
})

module.exports = router